package com.diudiutechnology.www.yadiudiu.util.http;
/**
 * 基本业务类，全局公共方法
 */

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class BaseBusiness {

    public static String getResponseMsg(JSONObject response) {
        return getResponseMsg(response.toString());
    }

    public static String getResponseMsg(String response) {
        String errorMsg = "数据有误";
        if (TextUtils.isEmpty(response)) {
            return errorMsg;
        }
        try {
            Type type = new TypeToken<BaseResponseBean<Object>>() {
            }.getType();
            BaseResponseBean<Object> bean = new Gson().fromJson(
                    response.toString(), type);
            if (!TextUtils.isEmpty(bean.mssage) && bean.error > 0) {
                errorMsg = bean.mssage;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return errorMsg;
    }

    /**
     * since 3.0
     *
     * @param response
     * @return
     */
    public static int getReponseCode(JSONObject response) {
        return getReponseCode(response.toString());
    }

    /**
     * since 3.0
     *
     * @param response
     * @return
     */
    public static int getReponseCode(String response) {
        int code = -1;
        if (TextUtils.isEmpty(response)) {
            return code;
        }
        try {
            JSONObject json = new JSONObject(response);
            code = json.optInt("Code", -100);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return code;
    }

    /**
     * 验证手机号是否合法
     *
     * @param phone
     * @return
     */
    public static boolean checkPhoneNum(String phone) {
        Pattern p = Pattern.compile("^1[3,4,5,7,8]\\d{9}$");
        Matcher m = p.matcher(phone);
        return m.matches();
    }

    /**
     * 验证手机号是否支持短信验证 180,181不支持
     *
     * @param phone
     * @return
     */
    public static boolean checkPhoneNumCanAuth(String phone) {
        Pattern p = Pattern.compile("^1[8][0,1]\\d{8}$");
        Matcher m = p.matcher(phone);
        return !m.matches();
    }

    /**
     * 检测普通电话号码是否符合规则（仅检测位数）
     */
    public static boolean checkNormalPhone(String phone) {
        int length = phone.length();
        if (length == 7 || length == 8 || length == 10 || length == 11
                || length == 12) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean checkUserPhone(String phone) {
        int length = phone.length();
        if (length == 11) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 检验邮箱地址是否正确
     */
    public static boolean checkEmail(String email) {
        Pattern p = Pattern
                .compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
        Matcher m = p.matcher(email);
        return m.matches();
    }

    /**
     * 检验QQ号码是否合法
     */
    public static boolean checkQQ(String qq) {
        Pattern p = Pattern.compile("^[1-9]\\d{4,10}$");
        Matcher m = p.matcher(qq);
        return m.matches();
    }

    /**
     * 电话号码转换为十六进制字符串
     *
     * @param numStr
     * @return
     */
    public static String numToHexString(String numStr) {
        String hexString = "";
        try {
            hexString = Long.toHexString(Long.valueOf(numStr));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return hexString;
    }

    public static BaseResponseBean<Object> paseBase(String reponse) {
        try {
            return new Gson().fromJson(reponse,
                    new TypeToken<BaseResponseBean<Object>>() {
                    }.getType());
        } catch (Exception e) {
            return null;
        }
    }
}
